define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'qrcode', 'layer', 'upload'], function ($, undefined, Backend, Table, Form, qrcode, layer, Upload) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    ajaxOperate : ['add','edit']
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                pk: 'id',
                sortName: 'id',
                queryParams:function(params) {
                    // 要显示的字段
                    // params.fields = '';
                    // // 要关联的拓展信息
                    params.extra = 'stock_info,product_info,use_user_info,order_info';
                    return params;
                },
                // search:true,
                // commonSearch: true,
                // searchFormVisible: false,
                columns: [
                    [
                        // {field: 'state', checkbox: true, },
                        {field: 'id', title: __('ID'),operate:false},
                        {field: 'stock_info.title', title: '所属批次',align:'left',operate:"LIKE %...%", formatter: Controller.api.formatter.remark},
                        {field: 'product_info.name', title: '关联产品',operate: false},
                        {field: 'order_info.sn', title: '关联订单',operate: false},
                        {field: 'use_user_info.nickname', title: '关联用户',operate: false},
                        {field: 'status', title: '状态', formatter: function (value, row, index) {
                            return (row.status == 1) ? '启用' : '禁用';
                            },operate:false},
                        {field: 'create_time', title: '创建时间', formatter: Table.api.formatter.datetime,operate:false},
                        {field: 'update_time', title: '更新时间', formatter: Table.api.formatter.datetime,operate:false},
                        {field: 'operate', title: __('Operate'), events: Controller.api.events.operate, formatter: Controller.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add:function () {
            Controller.api.bindevent();
        },
        edit:function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
                $(document).on("click", ".fieldlist .append", function () {
                    var rel = parseInt($(this).closest("dl").attr("rel")) + 1;
                    var name = $(this).closest("dl").data("name");
                    $(this).closest("dl").attr("rel", rel);
                    $('<dd class="form-inline"><input type="text" name="' + name + '[' + rel + ']" class="form-control" value="" size="50" /> <span class="btn btn-sm btn-danger btn-remove"><i class="fa fa-times"></i></span> <span class="btn btn-sm btn-primary btn-dragsort"><i class="fa fa-arrows"></i></span></dd>').insertBefore($(this).parent());
                });
                $(document).on("click", ".fieldlist dd .btn-remove", function () {
                    $(this).parent().remove();
                });


                //拖拽排序
                require(['dragsort'], function () {
                    //绑定拖动排序
                    $("dl.fieldlist").dragsort({
                        itemSelector: 'dd',
                        dragSelector: ".btn-dragsort",
                        dragEnd: function () {

                        },
                        placeHolderTemplate: "<dd></dd>"
                    });
                });

            },
            formatter:{
                operate: function (value, row, index) {
                    var _operate = '';
                        // _operate += "<a href='javascript:;' data-url='admin/index/changestatus' class='btn btn-" + (row.status == 1 ? "danger" : "info") + " btn-xs btn-change' data-id='" + row.id + "' data-params='" + (row.status == '1' ? '0' : '1') + "'>" + (row.status == '1' ? __('下架') : __('上架')) + "</a>&nbsp;";
                    return _operate + Table.api.formatter.operate(value, row, index, $("#table"));
                }
            },
            events:{
                operate: $.extend({
                }, Table.api.events.operate)
            }
        }
    }
    return Controller;
});
